#line 1 "..\\SourceCode\\Util\\source\\util.buffer.c"
#line 1 "..\\SourceCode\\target\\include\\types.h"



typedef unsigned char  	U8;
typedef char  			S8;
typedef unsigned short 	U16;
typedef short 			S16;
typedef unsigned long  	U32;
typedef long  			S32;
typedef unsigned char  	BOOL;

typedef U32				BASE_TYPE;

typedef U32				SIZE_TYPE;













#line 2 "..\\SourceCode\\Util\\source\\util.buffer.c"
#line 1 "..\\SourceCode\\Util\\include\\util.buffer.h"



typedef struct {
	U32 length;
	U32 size;
	U32 writingPointer;
	U32 readingPointer;
	U8 *data;
} Buffer;

extern void util_buffer_init(Buffer *buffer, U32 length, U8 *data);
extern void util_buffer_clear(Buffer *buffer);
extern BOOL util_buffer_insert(Buffer *buffer, U8 byte);
extern BOOL util_buffer_remove(Buffer *buffer, U8 *byte);

#line 3 "..\\SourceCode\\Util\\source\\util.buffer.c"

void util_buffer_init(Buffer *buffer, U32 length, U8 *data)
{
	buffer->length = length;
	buffer->data = data;
	util_buffer_clear(buffer);
}

void util_buffer_clear(Buffer *buffer)
{
	buffer->writingPointer = 0;
	buffer->readingPointer = 0;
	buffer->size = 0;
}

BOOL util_buffer_insert(Buffer *buffer, U8 byte)
{
	if(buffer->size < buffer->length)
	{
		buffer->data[buffer->writingPointer++] = byte;

		if(buffer->writingPointer == buffer->length){
			buffer->writingPointer = 0;
		}

		buffer->size++;

		return (1);
	}
	else{
		return (0);
	}
}

BOOL util_buffer_remove(Buffer *buffer, U8 *byte)
{
	if(buffer->size > 0)
	{
		*byte = buffer->data[buffer->readingPointer];

		buffer->readingPointer++;

		if(buffer->readingPointer == buffer->length)
		{
			buffer->readingPointer = 0;
		}

		buffer->size--;

		return (1);
	}
	else
	{
		return (0);
	}
}
